******
*Clean dataset and create further exposure measures for the individual level analysis
******

use "W:\Matti M\Replication\municipality_shares_bartik_1939.dta" , clear
keep  bartik*  mun39  individual ln_area ln_pop_1940 manushare_1940  agrishare_1940 share_service_1940 ///
share_construction_1940  displaced_share_1940 swedish_share_1940 average_incometax_38 income_tax_payers38 ln_arable_1940  ln_salary ///
relative_cows1940 relative_tractors1940 displaced_share_1940 swedish_share_1940  railway laani50 city town  changeagri change_tax_payers 
xtile  manu_quartile=manushare_1940 , nq(4)
xtile  agri_quartile=agrishare_1940 , nq(4)
drop bartik_standardized 
egen bartik_standardized=std(bartik) 
egen bartiknotimber_st=std(bartiknotimber)
egen bartikhs_st=std(bartikhs)
egen bartikls_st=std(bartikls)
xtile  bartik_cluster=bartik_standardized , nq(50)
xtile  bartik_decile=bartik_standardized , nq(10)
xtile  bartik_quartile=bartik_standardized , nq(4)
xtile  bartik_h=bartik_standardized , nq(2)
gen bartik_high=bartik_h==2
gen bartikq2=(bartik_quartile==2)
gen bartikq3=(bartik_quartile==3)
gen bartikq4=(bartik_quartile==4)

save "$temp/shiftshare.dta", replace


******
*Import the municipality shares to create local Bartik shocks
* to fit the balanced local datasets
******

use "$temp/municipality_shares_bartik_1939.dta", clear

drop code

*Fix some municipal codes in the file to fit the alternative balanced dataset

destring mun39, gen(code)
replace code=153 if code==700
replace code=573 if code==579 // parainen parainen
replace code=853 if code==477  // maaria turku 
replace code=851 if code==7  // tornio ala
replace code=858 if code==186  // tuusula järvenpää
replace code=311 if code==201  // karhula kymi 
replace code=430 if code==431  // loimaat 
replace code=220 if code==221  // karjaat 

rename individual wei

collapse (sum) wei (max) railway town city (mean) share_ind*  laani50   ln_area  share_service_1940 income_tax_payers38 ln_arable_1940 relative_cows1940 relative_tractors1940 displaced_share_1940 swedish_share_1940  [aw=wei], by(code)

*Calcualte Bartik shocks again with these data as L_im/L_m x Reparations/L_i  

gen bartik2=(share_ind4*1.16)+(share_ind19*.148)+(share_ind25*15.02)+(share_ind27*38.10)+(share_ind31*.06)+(share_ind32*1.33)+(share_ind33*1.40)+(share_ind34*4.21)+(share_ind35*20.96)+(share_ind36*75.74)+(share_ind37*149.29)+(share_ind38*192.77)+(share_ind39*.13) 


*Higs skill

gen bartik_h=(share_ind27*34.13)+(share_ind31*.04)+(share_ind32*1.33)+(share_ind36*73.455)+(share_ind37*159.501)+(share_ind38*192.221)+(share_ind39*.136) 

*Low skill 

gen bartik_l=(share_ind4*1.16)+(share_ind19*.14)+(share_ind25*4.87)+(share_ind33*1.35)+(share_ind34*3.01)+(share_ind35*15.55) 

**Without timber and paper

gen bartikntp=(share_ind4*1.16)+(share_ind19*.14)+(share_ind31*.06)+(share_ind32*1.33)+(share_ind33*1.40)+(share_ind34*4.20)+(share_ind35*20.96)+(share_ind36*75.74)+(share_ind37*149.29)+(share_ind38*192.77)+(share_ind39*.13)  

gen bartik_h2=(share_ind31*.04)+(share_ind32*1.33)+(share_ind36*73.455)+(share_ind37*159.501)+(share_ind38*192.221)+(share_ind39*.136)

gen bartik_l2=(share_ind4*1.16)+(share_ind19*.14)+(share_ind33*1.35)+(share_ind34*3.01)+(share_ind35*15.55) 

*Standardize the variables 
egen bartik_st=std(bartik2)
egen bartik_ntp_st=std(bartikntp)
egen bartik_st_L=std(bartik_l)
egen bartik_st_H=std(bartik_h)
egen bartik_st_Lntp=std(bartik_l2)
egen bartik_st_Hntp=std(bartik_h2)

keep code wei town city laani50 ln_area share_service_1940 income_tax_payers38 ln_arable_1940 relative_cows1940 relative_tractors1940 displaced_share_1940 swedish_share_1940 bartik_st bartik_ntp_st bartik_st_H bartik_st_L bartik_st_Lntp bartik_st_Hntp bartik2

label variable code "Municipality code"
label variable wei "Weight"
label variable town "Town"
label variable city "City"
label variable laani50 "Region 1950"
label variable ln_area "Log of the area size"
label variable share_service_1940 "Share of service sector in 1940"
label variable income_tax_payers38 "Income tax payer share in 1938"
label variable ln_arable_1940 "Log of arable land in 1940"
label variable relative_cows1940 "Relative number of cows in 1940"
label variable relative_tractors1940 "Relative number of tractors in 1940"
label variable displaced_share_1940 "Share of displaced people in 1940"
label variable swedish_share_1940 "Share of Swedish speakers in 1940"
label variable bartik_st "Standardized exposure"
label variable bartik_ntp_st "Standardized exposure wo established"
label variable bartik_st_H "Standardized exposure hs"
label variable bartik_st_L "Standardized exposure ls"
label variable bartik_st_Hntp "Standardized exposure hs wo established"
label variable bartik_st_Lntp "Standardized exposure ls wo established"
label variable bartik2 "Shift share exposure"

save "$temp/bartik_shocks.dta", replace

